package com.lemon.funplus;

import java.util.ArrayList;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.lemon.funplus.adapter.HomeAdapter;
import com.lemon.funplus.adapter.NhacHaiAdapter;
import com.lemon.funplus.adapter.TruyenAdapter;
import com.lemon.funplus.object.HomeObject;
import com.lemon.funplus.parse.CMD;
import com.lemon.funplus.parse.JsonParser;
import com.lemon.funplus.ulti.BaseEngine;
import com.lemon.funplus.view.MaterialSection;

public class NhacHaiActivity extends BaseActivity implements OnScrollListener {
	private static final String TAG = NhacHaiActivity.class.getSimpleName();
	public static final String CATE_ID = "cate_id";
	public static final String CATE_TITLE = "cate_title";
	public static final String CATE_SECTION = "cate_section";
	String CATEID = "5";
	String TITLE = "Nhạc hài";
	String Section;
	String pagelimit = "20";

	ListView listView;
	ArrayList<HomeObject> homeObjects;
	int m_PreviousTotalCount = 0;
	NhacHaiAdapter adapter;
	ProgressBar progressBar;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.home_layout);

		Bundle extras = getIntent().getExtras();
		if (getIntent().hasExtra(CATE_ID))
			CATEID = extras.getString(CATE_ID);
		if (getIntent().hasExtra(CATE_TITLE))
			TITLE = extras.getString(CATE_TITLE);
		if (getIntent().hasExtra(CATE_SECTION))
			Section = extras.getString(CATE_SECTION);
		homeObjects = new ArrayList<HomeObject>();

		initMain();
		adapter = new NhacHaiAdapter(NhacHaiActivity.this, homeObjects);
		listView.setAdapter(adapter);
		new AsyncTaskParseJson().execute("");
	}

	void initMain() {
		listView = (ListView) findViewById(R.id.listViewHome);
		progressBar = (ProgressBar) findViewById(R.id.progressBar_home);
		listView.setEmptyView(progressBar);
		listView.setOnScrollListener(this);
	}

	@Override
	protected void onResume() {
		super.onResume();
		if (Section != null && !Section.equals("")) {
			menuFragment.currentSection = menuFragment.getSection(Section);
			menuFragment.getSection(Section).select();
		} else {
			menuFragment.currentSection = menuFragment.HOME;
			menuFragment.HOME.select();
		}
		myActionBar.setTitle(TITLE);
	}

	// private static long back_pressed = 0;
	//
	// @Override
	// public void onBackPressed() {
	// if (back_pressed + 2000 > System.currentTimeMillis())
	// super.onBackPressed();
	// else
	// Toast.makeText(getBaseContext(), "Nhấn back lần nữa để thoát!",
	// Toast.LENGTH_SHORT).show();
	// back_pressed = System.currentTimeMillis();
	// }

	public String home(String key, String page, String limit) {
		String body = String.format(CMD.HOME, key, page, limit);
		String url = String.format(CMD.URL_MAIN, body);
		Log.e("getJson", "url: " + url);
		return url;
	}

	public class AsyncTaskParseJson extends AsyncTask<String, String, String> {
		// ProgressDialog progressDialog = new
		// ProgressDialog(AccountLoginActivity.this);

		@Override
		protected void onPreExecute() {
			// if (!progressDialog.isShowing())
			// progressDialog.show();
		}

		@Override
		protected String doInBackground(String... arg0) {
			// Log.e("totalItemCount", "totalItemCount: " +
			// m_PreviousTotalCount);
			int pagesize = Integer.parseInt(pagelimit);
			String page = Integer.toString(m_PreviousTotalCount / pagesize + 1);
			String str = JsonParser.catechild(CATEID, page, "20");
			return str;
		}

		@Override
		protected void onPostExecute(String result) {
			// BaseEngine.showLogformLongString("", result);
			if (result != null)
				try {
					ArrayList<HomeObject> list = JsonParser.readHome(result);
					for (int i = 0; i < list.size(); i++) {
						homeObjects.add(list.get(i));
					}
					if (adapter != null)
						adapter.notifyDataSetChanged();
				} catch (Exception e) {
					e.printStackTrace();
				}
		}
	}

	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {

	}

	@Override
	public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
		if (totalItemCount == 0 || adapter == null)
			return;
		if (m_PreviousTotalCount == totalItemCount) {
			return;
		}
		boolean loadMore = firstVisibleItem + visibleItemCount >= totalItemCount - 3;
		if (loadMore && firstVisibleItem > 0 && totalItemCount - m_PreviousTotalCount >= 20) {
			m_PreviousTotalCount = totalItemCount;
			new AsyncTaskParseJson().execute("");
		}
	}

}
